﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SBPweb.PROvibe.Shared.Library.Base
{

    public static class IIndexableExtensions
    {

        #region Contains

        public static bool ContainsId(this IEnumerable<IIndexable> element, int index)
        {
            return ContainsId<IIndexable>(element, index);
        }

        public static bool ContainsId<TSource>(this IEnumerable<TSource> element, int index)
            where TSource : IIndexable
        {
            return element.Any(x => x.Id == index);
        }

        #endregion

        #region First

        public static IIndexable FirstById(this IEnumerable<IIndexable> element, int index)
        {
            return FirstById<IIndexable>(element, index);
        }

        public static TResult FirstById<TResult>(this IEnumerable<IIndexable> element, int index)
            where TResult : IIndexable
        {
            return FirstById<IIndexable, TResult>(element, index);
        }

        public static TResult FirstById<TSource, TResult>(this IEnumerable<TSource> element, int index)
            where TSource : IIndexable
            where TResult : TSource
        {
            return (TResult)element.FirstOrDefault(x => x.Id == index);
        }

        #endregion

        #region Where

        public static IEnumerable<IIndexable> WhereIdIs(this IEnumerable<IIndexable> element, int index)
        {
            return WhereIdIs<IIndexable>(element, index);
        }

        public static IEnumerable<TResult> WhereIdIs<TResult>(this IEnumerable<IIndexable> element, int index)
            where TResult : IIndexable
        {
            return WhereIdIs<IIndexable, TResult>(element, index);
        }

        public static IEnumerable<TResult> WhereIdIs<TSource, TResult>(this IEnumerable<TSource> element, int index)
            where TSource : IIndexable
            where TResult : TSource
        {
            return element.Where(x => x.Id == index).Cast<TResult>().ToList();
        }

        #endregion

    }

}